package com.leyou.page.web;

import com.leyou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

/**
 * @author 虎哥
 */
@Controller
public class PageController {

    @Autowired
    private PageService pageService;

    /**
     * 跳转到商品详情页
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/item/{id}.html")
    public String toItemHtml(@PathVariable("id")Long id, Model model){
        // 根据商品id，查询相关数据
        Map<String,Object> itemData = pageService.loadItemData(id);
        // 把这些数据存入Model中
        model.addAllAttributes(itemData);

        // 生成静态页
        pageService.asyncCreateItemHtml(id);

        // 跳转到商品的视图
        return "item";
    }
}
